/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package Action;

import Entity.TicketsCategory;
import java.util.List;
import javax.ejb.Stateless;
import javax.persistence.EntityManager;
import javax.persistence.PersistenceContext;

/**
 *
 * @author Cyrax
 */
@Stateless
public class TicketsCatSessionBean implements TicketsCatSessionBeanLocal {

    @PersistenceContext(unitName = "EasyTickets-ejbPU")
    private EntityManager em;

    public void persist(Object object) {
        em.persist(object);
    }

    @Override
    public List<TicketsCategory> listAll() {

     
            List<TicketsCategory> tkCat;
            tkCat = em.createNamedQuery("TicketsCategory.findAll").getResultList();
            return tkCat;
      

    }

    @Override
    public void addTicketsCat(TicketsCategory tkCat) {
        try {
            em.persist(tkCat);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    
    @Override
    public void updateTicketsCat(int tcId, String tcName,double tcPrice) {
       TicketsCategory tkCat = em.find(TicketsCategory.class, tcId);
        if (tkCat != null) {
             
              tkCat.setTcName(tcName);
              tkCat.setPrice(tcPrice);
            em.persist(tkCat);
        } else {
            System.out.println("Not found tcId  to Update");
        }
    }

    @Override
    public void DeleteTicketsCat(TicketsCategory tkCat) {

        if (tkCat != null) {
           em.remove(em.merge(tkCat));
        } else {
            System.out.println("Not found tkCat to delete");
        }
    }
   @Override
   public List<TicketsCategory> searchTicketsCategory(String tcName)
   {
        List<TicketsCategory> tcat=null;

        tcat = em.createQuery("SELECT t FROM TicketsCategory t WHERE t.tcName like :tcName").setParameter("tcName", "%" + tcName + "%").getResultList();
        System.out.println("eventTitle : " + tcName);


        return tcat;
   }

    @Override
    public void ticketsCatUpdate(int tkId, String tkName, double tkPrice) {
        TicketsCategory tkCat = em.find(TicketsCategory.class, tkId);
        if (tkCat != null) {

              tkCat.setTcName(tkName);
              tkCat.setPrice(tkPrice);
             em.persist(tkCat);
        } else {
            System.out.println("Not found tcId  to Update");
        }
    }

    boolean result=false;
      @Override
    public boolean checkTicketsCat(String name)
    {
      List<TicketsCategory> catList=null;
         catList=em.createQuery("SELECT t FROM TicketsCategory t").getResultList();
         for(TicketsCategory cat: catList)
         {
           if(cat.getTcName().equals(name))
           {
              result=true;
              break;
           }else
           {
               result=false;
           }
         }
        return  result;
    }
}
